<?php

namespace App\Admin\Controllers;

use App\Member;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户列表');
            $content->description('用户列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户列表');
            $content->description('用户列表');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Member::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->id('ID')->sortable();
            $grid->nickname('用户昵称');
            $grid->headimgurl('用户头像')->display(
                function ($value){
                    return "<img src='{$value}' height='30px'>";
                }
            );
            $grid->status('状态')->display(
                function($value){
                    if($value==1){
                        return "<a class='btn btn-xs btn-success'>启用</a>";
                    }else{
                        return "<a class='btn btn-xs btn-warning'>禁用</a>";
                    }
                }
            );
            $grid->column('leader.nickname','推广人')->display(function($value){
               return  $value ?? '无推广人';
            });

            $grid->truename('真实姓名');
            $grid->mobile('手机号');
            $grid->column('id_number','身份证号');
            $grid->role('身份')->display(function($value){
                return $value==1 ? '个人' : '旅行社';
            });
            $grid->wallet_all_push_money('总提成')->sortable();
            $grid->wallet_push_money('剩余提成')->sortable();
            $grid->wallet_used_push_money('已使用提成')->sortable();
            $grid->created_at('注册时间');
            $grid->actions(function($actions){
                $actions->disableDelete();
            });

            $grid->disableExport();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->filter(function(Grid\Filter $filter){
                $filter->like('nickname','用户昵称');
                $filter->like('truename','真实姓名');
                $filter->like('id_number','身份证号');
                $filter->equal('mobile', '手机号');
                $filter->equal('status','状态')->radio([
                    ''   => '全部',
                    1    => '启用',
                    2    => '禁用',
                ]);
                $filter->equal('is_worker','工作人员')->radio([
                    '' =>'全部',
                    0=>'是',
                    1=>'否'
                ]);
                $filter->equal('role','角色')->radio([
                    '' =>'全部',
                    0=>'个人',
                    1=>'旅行社'
                ]);
                $filter->equal('is_display','禁止查看提成')->radio([
                    '' =>'全部',
                    1=>'关闭',
                    2=>'开启'
                ]);
                $filter->where(function ($query) {
                    $query->whereHas('leader', function ($query) {
                        $query->where('nickname', 'like', "%{$this->input}%");
                    });

                }, '推广人昵称');
                $filter->between('created_at', '注册时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Member::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('truename','真实姓名');
            $form->currency('wallet_all_push_money','总提成')->symbol("元");
            $form->currency('wallet_push_money','剩余提成')->symbol("元");
            $form->currency('wallet_used_push_money','已使用提成')->symbol("元");
            $form->text('wallet_spots','景点次数');
            $states = [
                'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '禁用', 'color' => 'danger'],
            ];
            $form->switch('status','状态')->states($states);
            $role=[
                'on'  => ['value' => 1, 'text' => '个人', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '旅行社', 'color' => 'success'],
            ];
            $form->switch('role','角色')->states($role);
            $woker=[
                'on'  => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            ];
            $form->switch('is_worker','设为工作人员')->states($woker);
            $display=[
                'on'  => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            ];
            $form->switch('is_display','禁止查看提成')->states($display);
        });
    }
}
